package ru.amse.tsyganov.jumleditor.commands;

import ru.amse.tsyganov.jumleditor.model.StateVertex;
import ru.amse.tsyganov.jumleditor.model.Transition;
import ru.amse.tsyganov.jumleditor.view.TransitionView;

/*package*/ class MoveTransitionCommand extends Command {
	
	private final TransitionView<? extends Transition> movingTransition;
	
	private final StateVertex oldSourceVertex;
	
	private final StateVertex oldTargetVertex;
	
	private final StateVertex newSourceVertex;
	
	private final StateVertex newTargetVertex;

	public MoveTransitionCommand(TransitionView<? extends Transition> movingTransition, 
			StateVertex newSourceVertex, StateVertex newTargetVertex) {
		
		if (movingTransition == null || newSourceVertex == null ||
				newTargetVertex == null || newSourceVertex == newTargetVertex) {
			throw new IllegalArgumentException();
		}
		
		this.movingTransition = movingTransition;
		oldSourceVertex = movingTransition.getModel().getSource();
		oldTargetVertex = movingTransition.getModel().getTarget();
		this.newSourceVertex = newSourceVertex;
		this.newTargetVertex = newTargetVertex;
		
		if (oldSourceVertex == newSourceVertex &&
				oldTargetVertex == newTargetVertex) {
			
			throw new IllegalArgumentException();
		}
	}
	
	@Override
	public void execute() {
		movingTransition.getModel().setSource(newSourceVertex);
		movingTransition.getModel().setTarget(newTargetVertex);
	}

	@Override
	public void unexecute() {
		movingTransition.getModel().setSource(oldSourceVertex);
		movingTransition.getModel().setTarget(oldTargetVertex);
	}

}
